/**
 * 
 */
package com.arsl.client.view;

import com.arsl.client.presenter.Presenter;
import com.arsl.client.presenter.SignInPanelPresenter;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author NuwanSam
 * 
 */
public class SignInPanel extends Composite implements HasPresenter {

	private static SignInPanelUiBinder uiBinder = GWT
			.create(SignInPanelUiBinder.class);
	private SignInPanelPresenter presenter;

	interface SignInPanelUiBinder extends UiBinder<Widget, SignInPanel> {
	}

	/**
	 * Because this class has a default constructor, it can be used as a binder
	 * template. In other words, it can be used in other *.ui.xml files as
	 * follows: <ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
	 * xmlns:g="urn:import:**user's package**">
	 * <g:**UserClassName**>Hello!</g:**UserClassName> </ui:UiBinder> Note that
	 * depending on the widget that is used, it may be necessary to implement
	 * HasHTML instead of HasText.
	 */
	public SignInPanel() {
		initWidget(uiBinder.createAndBindUi(this));
		txtEmail.setText("admin@lh.com");
		txtPass.setText("admin");
	}

	@UiField
	Button btnSignIn;

	@UiField
	TextBox txtEmail;

	@UiField
	PasswordTextBox txtPass;

	@UiHandler("txtPass")
	public void onKeyDown(KeyPressEvent event) {
		if (event.getCharCode() == KeyCodes.KEY_ENTER) {
			trySignIn();
		}
	}

	@UiHandler("btnSignIn")
	void onClick(ClickEvent e) {
		trySignIn();
	}

	private void trySignIn() {
		presenter.trySignIn(txtEmail.getText(), txtPass.getText());

	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = (SignInPanelPresenter) presenter;
		// trySignIn();
	}

}
